package ldkxingzhe.ldkxingzhedemo.others;

import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.io.File;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;


/**
 * Created by xingzhe on 15/10/9.
 */
public class MediaRecorderVideoActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MediaRecorderVideoActivity.class.getSimpleName();

    private Button start, stop;
    private Camera camera;
    private SurfaceView surfaceView;
    private MediaRecorder mediaRecorder;
    private boolean isRecording;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_recorder_video);

        start = (Button)findViewById(R.id.start);
        stop = (Button)findViewById(R.id.stop);
        surfaceView = (SurfaceView)findViewById(R.id.surface_view);

        stop.setEnabled(false);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);

        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                start();
                break;
            case R.id.stop:
                stop();
                break;
            default:
                throw new IllegalStateException("id is not found");
        }
    }

    private void start(){
        try{
            File file = new File(getExternalCacheDir(),"test.mp4");
            if(file.exists()){
                file.delete();
            }
            new Thread(){
                @Override
                public void run() {
                    if(prepareVideoRecorder()){
                        mediaRecorder.start();
                        isRecording = true;
                    }else{
                        LogDemo.e(TAG,"prepareVideoRecorder failed");
                    }
                }
            }.start();

            start.setEnabled(false);
            stop.setEnabled(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private boolean prepareVideoRecorder(){
        camera = getCameraInstance();
        camera.setDisplayOrientation(90);
        mediaRecorder = new MediaRecorder();

        camera.unlock();
        mediaRecorder.setCamera(camera);

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
        profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
//        profile.audioCodec = MediaRecorder.AudioEncoder.DEFAULT;
        profile.videoCodec = MediaRecorder.VideoEncoder.DEFAULT;
        profile.videoFrameRate = 10;
        mediaRecorder.setProfile(profile);
        mediaRecorder.setOutputFile(new File(getExternalCacheDir(), "test.mp4").getAbsolutePath());
        mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

        try{
            mediaRecorder.prepare();
        }catch (Exception e){
            LogDemo.e(TAG, "" + e.getMessage());
            releaseMediaRecorder();
        }
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaRecorder();
        releaseCamera();
    }

    private void releaseMediaRecorder(){
        if(mediaRecorder != null){
            mediaRecorder.reset();
            mediaRecorder.release();
            mediaRecorder = null;
            camera.lock();
        }
    }
    private void releaseCamera(){
        if(camera != null){
            camera.release();
            camera = null;
        }
    }
    private void stop(){
        if(isRecording){
            mediaRecorder.stop();
            releaseMediaRecorder();
            isRecording = false;

            start.setEnabled(true);
            stop.setEnabled(false);
        }
    }

    public static Camera getCameraInstance(){
        Camera c = null;
        try{
            c = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        }catch (Exception e){
            e.printStackTrace();
        }
        return c;
    }
}
